package com.jojo.jojomall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jojo.common.constant.ProductConstant;
import com.jojo.common.utils.BeanCopyUtils;
import com.jojo.jojomall.product.dao.*;
import com.jojo.jojomall.product.entity.*;
import com.jojo.jojomall.product.service.AttrAttrgroupRelationService;
import com.jojo.jojomall.product.service.CategoryService;
import com.jojo.jojomall.product.vo.AttrResVo;
import com.jojo.jojomall.product.vo.AttrVo;
import com.jojo.jojomall.product.vo.BaseAttrListVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jojo.common.utils.PageUtils;
import com.jojo.common.utils.Query;

import com.jojo.jojomall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
@Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
@Autowired
    CategoryDao categoryDao;
@Autowired
    AttrGroupDao attrGroupDao;
@Autowired
    CategoryService categoryService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }
@Transactional//保证一个事务的进行，如果有人出错，就不发生任何改变，直接回滚
    @Override
    public void saveAttr(AttrVo attr) {

        AttrEntity attrEntity = BeanCopyUtils.copyBean(attr, AttrEntity.class);
        this.save(attrEntity);
        //保存完基本步骤以后，然后我们应该改掉联合表的关系
        if(attrEntity.getAttrType()==ProductConstant.AttrEnum.Attr_TYPE_BASE.getCode()){
        attrAttrgroupRelationService.saveattr(attr.getAttrGroupId(),attr.getAttrId());}

    }
//获取分类规格参数
    @Override
    public PageUtils querListPage(Map<String, Object> params, Long catelogId, String type) {
        LambdaQueryWrapper<AttrEntity> qw = new LambdaQueryWrapper<>();
        qw.eq(AttrEntity::getAttrType,"base".equalsIgnoreCase(type)?ProductConstant.AttrEnum.Attr_TYPE_BASE:ProductConstant.AttrEnum.ATTR_TYPE_SALE);
        if (catelogId!=0){
            qw.eq(AttrEntity::getCatelogId,catelogId);
        }
        String key =(String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            qw.eq(AttrEntity::getAttrId,key).or().like(AttrEntity::getAttrName,key);
        }
//        进行分页查询；
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
               qw
        );
        //拿到全部查到的结果
        List<AttrEntity> records = page.getRecords();
        //转化为一个list
        List<BaseAttrListVo> baseAttrListVos = BeanCopyUtils.copyBeanList(records, BaseAttrListVo.class);
        List<BaseAttrListVo> collect = baseAttrListVos.stream().map(s -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntitybyid = attrAttrgroupRelationDao.selectOne(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrId, s.getAttrId()));
            if (!Objects.isNull(attrAttrgroupRelationEntitybyid)) {
                if ("base".equals(type)){
                Long attrGroupId = attrAttrgroupRelationEntitybyid.getAttrGroupId();
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectOne(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getAttrGroupId, attrGroupId));
                s.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
            CategoryEntity categoryEntity = categoryDao.selectById(s.getCatelogId());
            if (!Objects.isNull(categoryEntity)) {
                s.setCatelogName(categoryEntity.getName());
            }
            return s;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }
//查询属性详情
    @Override
    public AttrResVo getByIdVo(Long attrId) {
        AttrEntity byId = this.getById(attrId);
            AttrResVo attrResVo = BeanCopyUtils.copyBean(byId, AttrResVo.class);
            Long catelogId = attrResVo.getCatelogId();
            Long[] pathById = categoryService.findPathById(catelogId);
            if (pathById!=null){
                attrResVo.setCatelogPath(pathById);
            }
            return attrResVo;

    }
//  在指定的属性集合里面，挑出来检索属性；
    @Override
    public List<Long> selectSeachAttrs(List<Long> attrIdlist) {
        return baseMapper.selectSeachAttrs(attrIdlist);
    }

}
